package com.wandian.cqcsk.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;


/**
 * 我的打卡文字弹框
 */
public class MineDKTextDialog extends PopupWindow {
    private Activity mContent;
    private View mMenuView;
    public DialogViewClink dialogViewClink;
    public interface  DialogViewClink{
        void viewClink(String text);
    }

    public MineDKTextDialog(Activity context, DialogViewClink dialogViewClink) {
        super(context);
        this.mContent = context;
        this.dialogViewClink =dialogViewClink;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_mine_dk_text, null);

        TextView tv_save = (TextView)mMenuView.findViewById(R.id.tv_save);
        TextView tv_cancel =(TextView)mMenuView.findViewById(R.id.tv_cancel);
        EditText et_text =(EditText)mMenuView.findViewById(R.id.et_text);



        tv_save.setOnClickListener(v -> {
            finishSoftBoard();
            if(!UIUtils.isEmpty(et_text.getText())){
                dialogViewClink.viewClink(et_text.getText().toString());
            }else {
                dialogViewClink.viewClink("");
            }
            dismiss();

        });
        tv_cancel.setOnClickListener(v ->dismiss());


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);


    }

    //关闭软键盘
    private void finishSoftBoard() {
        InputMethodManager imm = (InputMethodManager)mContent.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&mContent.getCurrentFocus()!=null){
            if (mContent.getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(mContent.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}